#include "light_send_config.h"

void ws2812_rgb_transition_fun(u8 *data,u8 *send_data,u16 send_len);
void ws2812_grb_transition_fun(u8 *data,u8 *send_data,u16 send_len);
void ws2812_brg_transition_fun(u8 *data,u8 *send_data,u16 send_len);

LIGHT_SEND_CONFIG light_send_config[LIGHT_TYPE]=
{
    {}, //0
    {WS2812_RGB_SPEED,WS2812_RGB_TH,WS2812_RGB_TL}, //WS2812_RGB
    {WS2812_GRB_SPEED,WS2812_GRB_TH,WS2812_GRB_TL}, //WS2812_GBR
    {WS2812_BRG_SPEED,WS2812_BRG_TH,WS2812_BRG_TL}, //WS2812_GBR
};


LIGHT_TRANSITION_SEND_HANDLE_CONFIG light_transition_send_handle[LIGHT_TYPE]=
{
    {}, //0
    {ws2812_rgb_transition_fun}, //WS2812_RGB
    {ws2812_grb_transition_fun}, //WS2812_GBR
    {ws2812_brg_transition_fun}, //WS2812_BRG
};


void ws2812_rgb_transition_fun(u8 *data,u8 *send_data,u16 send_len)
{
    for(int i = 0 ; i<send_len; i+=3) //3个字节代表一个灯珠
    {
        for(int j = 0 ; j<3; j++) //重组32位
        {
            u8 cr=0;
            if (get_light_diretion() == FORWARD) //正向取值
            {
                cr= send_data[i+j]; //RGB取色
            }
            else if (get_light_diretion() == REVERSE) //反向取值
            {
                cr= send_data[send_len-i-3+j]; //RGB取色
            }
            
            u8 mask = 0x80;
            u8 high=0;
            
            if(0==j)
            {
                for(int k = 0; k<8; k++) 
                {
                    high = (mask&cr)?light_send_config[WS2812_RGB].high_soding:light_send_config[WS2812_RGB].low_coding;
                    mask>>=1;
                    data[(i+0)*8+k]=high;
                }
            }
            else if(1==j)
            {
                for(int k = 0; k<8; k++) 
                {   
                    high = (mask&cr)?light_send_config[WS2812_RGB].high_soding:light_send_config[WS2812_RGB].low_coding;
                    mask>>=1;
                    data[(i+2)*8+k]=high;
                }                
            }
            else if(2==j)
            {
                for(int k = 0; k<8; k++) 
                {
                    high = (mask&cr)?light_send_config[WS2812_RGB].high_soding:light_send_config[WS2812_RGB].low_coding;
                    mask>>=1;
                    data[(i+1)*8+k]=high;
                }
            }
        }
    }
}                                                                       

void ws2812_grb_transition_fun(u8 *data,u8 *send_data,u16 send_len)
{
    for(int i = 0 ; i<send_len; i+=3) //3个字节代表一个灯珠
    {
        for(int j = 0 ; j<3; j++) //重组32位
        {
            u8 cr= send_data[send_len-i-3+j]; //RGB取色
            u8 mask = 0x80;
            u8 high=0;
            if(1==j)
            {
                for(int k = 0; k<8; k++) 
                {
                    high = (mask&cr)?light_send_config[WS2812_RGB].high_soding:light_send_config[WS2812_RGB].low_coding;
                    mask>>=1;
                    data[(i+0)*8+k]=high;
                }
            }
            else if(0==j)
            {
                for(int k = 0; k<8; k++) 
                {   
                    high = (mask&cr)?light_send_config[WS2812_RGB].high_soding:light_send_config[WS2812_RGB].low_coding;
                    mask>>=1;
                    data[(i+1)*8+k]=high;
                }                
            }
            else if(2==j)
            {
                for(int k = 0; k<8; k++) 
                {
                    high = (mask&cr)?light_send_config[WS2812_RGB].high_soding:light_send_config[WS2812_RGB].low_coding;
                    mask>>=1;
                    data[(i+j)*8+k]=high;
                }
            }
        }
    }
}

void ws2812_brg_transition_fun(u8 *data,u8 *send_data,u16 send_len)
{
    for(int i = 0 ; i<send_len; i+=3) //3个字节代表一个灯珠
    {
        for(int j = 0 ; j<3; j++) //重组32位
        {
            u8 cr= send_data[send_len-i-3+j]; //RGB取色
            u8 mask = 0x80;
            u8 high=0;

            if(1==j)
            {
                for(int k = 0; k<8; k++) 
                {
                    high = (mask&cr)?light_send_config[WS2812_BRG].high_soding:light_send_config[WS2812_BRG].low_coding;
                    mask>>=1;
                    data[(i+0)*8+k]=high;
                }
            }
            else if(2==j)
            {
                for(int k = 0; k<8; k++) 
                {   
                    high = (mask&cr)?light_send_config[WS2812_BRG].high_soding:light_send_config[WS2812_BRG].low_coding;
                    mask>>=1;
                    data[(i+1)*8+k]=high;
                }                
            }
            else if(0==j)
            {
                for(int k = 0; k<8; k++) 
                {
                    high = (mask&cr)?light_send_config[WS2812_BRG].high_soding:light_send_config[WS2812_BRG].low_coding;
                    mask>>=1;
                    data[(i+j)*8+k]=high;
                }
            }
        }
    }
}


